clear
clear matrix
capture log close
set rmsg on
set more off
set more 1
set matsize 800
set scrollbufsize 500000
set mem 7g

********************************************************************************
*****                  Setting paths to file directories                    ****
********************************************************************************

* Desktop path - designate as needed *

global code "D:\XXX\code_for_publication"

********************************************************************************
*****                 Winsor program (winsor varname [1,5])                 ****
********************************************************************************

	capture program drop winsor
	program define winsor
		quiet sum `1', detail
		replace `1' = r(p1)  if `1' ~= . & (`1' < r(p1))  & `2' == 1
		replace `1' = r(p99) if `1' ~= . & (`1' > r(p99)) & `2' == 1
		replace `1' = r(p5)  if `1' ~= . & (`1' < r(p5))  & `2' == 5
		replace `1' = r(p95) if `1' ~= . & (`1' > r(p95)) & `2' == 5
	end

********************************************************************************
*****                 Trimming program (trim varname [1,5])                 ****
********************************************************************************

	capture program drop trim
	program define trim
		quiet sum `1', detail
		replace `1' = .  if `1' ~= . & (`1' < r(p1))  & `2' == 1
		replace `1' = . if `1' ~= . & (`1' > r(p99)) & `2' == 1
		replace `1' = .  if `1' ~= . & (`1' < r(p5))  & `2' == 5
		replace `1' = . if `1' ~= . & (`1' > r(p95)) & `2' == 5
	end	
	
	capture program drop trimbis
	program define trimbis
		local i=100-`2'
		local j=`2'
		centile  `1', centile(`i')
		replace `1'=. if `1'>=r(c_1)
		centile  `1', centile(`j')
		replace `1'=. if `1'<=r(c_1)
	end

********************************************************************************
*****                 Winsor program (rightwinsor varname [1,5])            ****
********************************************************************************

	capture program drop rwinsor
	program define rwinsor
		quiet sum `1', detail
		replace `1' = r(p99) if `1' ~= . & (`1' > r(p99)) & `2' == 1
		replace `1' = r(p95) if `1' ~= . & (`1' > r(p95)) & `2' == 5
	end
	
	
	
	
	
